package com.gxks.lhs.controller.user;


import com.gxks.lhs.constants.R;
import com.gxks.lhs.entity.AddressBook;
import com.gxks.lhs.security.JwtTokenProvider;
import com.gxks.lhs.security.SecurityUtils;
import com.gxks.lhs.service.IAddressBookService;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 地址管理 前端控制器
 * </p>
 *
 * @author LuoHaisong
 * @since 2023-06-05
 */
@RestController
@RequestMapping("/restful-api/addressBook")
public class AddressBookController {
    @Autowired
    private IAddressBookService addressBookService;


    @ApiOperation("添加收货地址")
    @PostMapping("/addAddress")
    public R addAddress(@RequestBody AddressBook addressBook) {
        addressBook.setUserId(SecurityUtils.getCurrentUserId());
        if (addressBookService.addAddress(addressBook)) {
            return R.success().setMsg("添加成功");
        }
        return R.error("添加失败");
    }

    @ApiOperation("删除收货地址")
    @DeleteMapping("/deleteAddress/{id}")
    public R deleteAddress(@PathVariable("id") Integer id) {
        String userId = SecurityUtils.getCurrentUserId();
        if (addressBookService.deleteAddress(userId, id)) {
            return R.success().setMsg("删除成功");
        }
        return R.error("删除失败");
    }

    @ApiOperation("修改收货地址")
    @PutMapping("/updateAddress")
    public R updateAddress(@RequestBody AddressBook addressBook) {
        addressBook.setUserId(SecurityUtils.getCurrentUserId());
        if (addressBookService.updateAddress(addressBook)) {
            return R.success().setMsg("修改收货地址成功");
        }
        return R.error("修改收货地址失败");
    }


    @ApiOperation("根据用户id查询收货地址")
    @GetMapping("/queryAddress")
    public R queryAddress() {
        String userId = SecurityUtils.getCurrentUserId();
        List<AddressBook> addressBookList = addressBookService.getAddressByUserId(userId);
        if (addressBookList != null) {
            return R.success(addressBookList, "查询成功");
        }
        return R.error("查询失败");
    }

    /**
     * 设置默认地址
     */
    @PutMapping("/defaultAddr/{addrId}")
    @Operation(summary = "设置默认地址", description = "根据地址id，设置默认地址")
    public R defaultAddr(@PathVariable("addrId") Long addrId) {
        String userId = SecurityUtils.getCurrentUserId();
        if (addressBookService.defaultAddr(userId, addrId)) {
            return R.success("修改地址成功");
        }
        return R.error("修改地址失败");
    }

    /**
     * 根据地址id查询地址
     */
    @GetMapping("/addrInfo/{id}")
    public R getAddress(@PathVariable("id") Integer id) {
        String userId = SecurityUtils.getCurrentUserId();
        AddressBook addressBook = addressBookService.getAddress(userId, id);
        if (addressBook != null) {
            return R.success(addressBook, "查询成功");
        }
        return R.error("查询失败");
    }

    /**
     * 获取用户的默认地址
     */
    @GetMapping("/defaultAddr")
    public R getDefaultAddr() {
        String userId = SecurityUtils.getCurrentUserId();
        AddressBook addressBook = addressBookService.getDefaultAddress(userId);
        if (addressBook != null) {
            return R.success(addressBook, "查询成功");
        }
        return R.error("查询失败");
    }

}

